package com.google.code.mochaccino.framework.transaction.provider;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import bitronix.tm.resource.jms.PoolingConnectionFactory;
import com.google.code.mochaccino.framework.transaction.jsr250.BitronixConnectionFactoryWrapper;
import com.google.common.base.Preconditions;
import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.jms.XAConnectionFactory;

/** Provider for a JMS Connection Factory. */
public class XAConnectionFactoryProvider extends XAResourceProvider<PoolingConnectionFactory,ConnectionFactory> {

	private Class<? extends XAConnectionFactory> connectionFactoryClass  = null;
	private Properties                           driverProperties        = null;
	private String                               password                = "password";
	private String                               username                = "username";
	private boolean                              cacheProducersConsumers = true;
	private boolean                              testConnections         = true;

	public XAConnectionFactoryProvider( String uniqueName, String username, String password, Class<? extends XAConnectionFactory> connectionFactoryClass, Properties driverProperties ) {
		super( Preconditions.checkNotNull( uniqueName ) );
		this.username = username;
		this.password = password;
		this.connectionFactoryClass = Preconditions.checkNotNull( connectionFactoryClass );
		this.driverProperties = Preconditions.checkNotNull( driverProperties );
	}

	@Override
	protected PoolingConnectionFactory createUnconfiguredResource() {
		return new PoolingConnectionFactory();
	}

	@Override
	public ConnectionFactory get() {
		PoolingConnectionFactory bean = super.createConfiguredResource();
		bean.setClassName( connectionFactoryClass.getName() );
		bean.setUser( username );
		bean.setPassword( password );
		bean.setTestConnections( testConnections );
		bean.setCacheProducersConsumers( cacheProducersConsumers );
		bean.setDriverProperties( driverProperties );

		return new BitronixConnectionFactoryWrapper( bean );
	}

	public void setCacheProducersConsumers( boolean cacheProducersConsumers ) {
		this.cacheProducersConsumers = cacheProducersConsumers;
	}

	public void setTestConnections( boolean testConnections ) {
		this.testConnections = testConnections;
	}
}
